﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>试题编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../static/css/font.css">
    <link rel="stylesheet" href="../../../static/css/weadmin.css">
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
    <div class="weadmin-body">
        <form class="layui-form">
            <div class="layui-form-item">
                <input type="text" id="EQID" name="EQID" autocomplete="off" class="layui-input">
                <label for="ETID" class="layui-form-label">
                    <span class="we-red">*</span>试题类型
                </label>
                <div class="layui-input-inline">
                    <select id="ETID" name="ETID" class="valid" lay-filter="ETID"></select>
                </div>
                <label for="EQSubject" class="layui-form-label">
                    <span class="we-red">*</span>知识点
                </label>
                <div class="layui-input-inline">
                    <select id="EQSubject" name="EQSubject" class="valid"></select>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EQContent" class="layui-form-label">
                    <span class="we-red">*</span>问题
                </label>
                <div class="layui-input-block">
                    <textarea id="EQContent" name="EQContent" placeholder="请输入" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EQScore" class="layui-form-label">
                    <span class="we-red">*</span>分数
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="EQScore" name="EQScore" lay-verify="required|number" autocomplete="off" class="layui-input">
                </div>
                <label for="EQIntegral" class="layui-form-label">
                    <span class="we-red">*</span>积分
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="EQIntegral" name="EQIntegral" lay-verify="required|number" autocomplete="off" class="layui-input">
                </div>
            </div>
            <!--<div class="layui-form-item">
                <label for="EQUseCount" class="layui-form-label">
                    <span class="we-red">*</span>使用次数
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="EQUseCount" name="EQUseCount" lay-verify="required|number" autocomplete="off" class="layui-input">
                </div>
                <label for="EQIsAutoMark" class="layui-form-label">给分方式</label>
                <div class="layui-input-inline" id="EQIsAutoMark">
                    <input type="radio" name="EQIsAutoMark" value="自动" title="自动" checked>
                    <input type="radio" name="EQIsAutoMark" value="人工" title="人工">
                </div>
            </div>-->
            <div class="layui-form-item" id="answerXuanZhe">
                <label for="EQAnswer" class="layui-form-label">
                    <span class="we-red">*</span>答案
                </label>
                <div class="layui-input-inline">
                    <select id="EQAnswerCheck" name="EQAnswerCheck" class="valid">
                    </select>
                </div>
            </div>
            <div class="layui-form-item" id="answerJieDa">
                <label for="EQAnswer" class="layui-form-label">
                    <span class="we-red">*</span>答案
                </label>
                <div class="layui-input-block">
                    <textarea id="EQAnswer" name="EQAnswer" placeholder="请输入" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EQContent" class="layui-form-label">

                </label>
                <button class="layui-btn" lay-filter="edit" lay-submit="">修 改</button>
            </div>
        </form>
    </div>
    <script src="../../../lib/layui/layui.js" charset="utf-8"></script>
    <script src="../../../static/js/common.js" charset="utf-8"></script>

    <script>

        layui.use(['form', 'jquery', 'layer', 'laydate'], function () {
            var form = layui.form,
                $ = layui.jquery,
                layer = layui.layer,
                laydate = layui.laydate;
            $.support.cors = true;
            $.ajaxSetup({
                cache: false,
                headers: {
                    "userid": JSON.parse(window.localStorage.authInfo).UserId,
                    "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                },
            })

            //试题类型变更事件 显示对应的答案界面
            $("#EQID").hide();
            $("#answerJieDa").hide();
            $("#answerXuanZhe").show();
            form.on('select(ETID)', function (data) {
                //获取选中的文本值
                //layer.msg($("#ETID").find("option:selected").text());
                //获取选中的value值
                //$("#ETID").val();
                var currETID = data.value; //$("#ETID").val();
                if (currETID == 1 || currETID == 2) {
                    $("#answerJieDa").hide();
                    $("#answerXuanZhe").show();
                    $("#EQAnswerCheck").empty();
                    if (currETID == 1) {
                        $("#EQAnswerCheck").append("<option value='A'>A</option>");
                        $("#EQAnswerCheck").append("<option value='B'>B</option>");
                        $("#EQAnswerCheck").append("<option value='C'>C</option>");
                        $("#EQAnswerCheck").append("<option value='D'>D</option>");
                    }
                    else {
                        $("#EQAnswerCheck").append("<option value='对'>对</option>");
                        $("#EQAnswerCheck").append("<option value='错'>错</option>");
                    }
                    form.render();
                } else {
                    $("#answerJieDa").show();
                    $("#answerXuanZhe").hide();
                }
            });

            $(function () {
                //加载试题类型信息
                var examquestions = $.ajax({
                    url: ApiUrl + "ExamType/SelList",
                    type: 'post',
                    success: function (data) {
                        if (data.StatusCode == 200 && data.Count != 0) {
                            $(data.Data).each(function (index, item) {
                                $("#ETID").append("<option value='" + item.ETID + "'>" + item.ETName + "</option>");
                            });
                            form.render();
                        }
                        else if (data.StatusCode == 403) {
                            top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Info);
                        }
                    }
                });
                //加载知识点信息
                var zhishidian = $.ajax({
                    url: ApiUrl + "KnowledgeKey/SelList",
                    type: 'post',
                    success: function (data) {
                        if (data.StatusCode == 200 && data.Count != 0) {
                            $(data.Data).each(function (index, item) {
                                $("#EQSubject").append("<option value='" + item.KKID + "'>" + item.KKName + "</option>");
                            });
                            form.render();
                        }
                        else if (data.StatusCode == 403) {
                            top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Info);
                        }
                    }
                });
                //加载试题信息
                var editdata = {};
                var infoajax = $.ajax({
                    url: ApiUrl + "ExamQuestions",
                    type: 'get',
                    data: { EQID: getUrlParam("EQID") },
                    success: function (data) {
                        if (data.StatusCode == 200 && data.Count != 0) {
                            editdata = data.Data;
                        }
                        else if (data.StatusCode == 403) {
                            top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Info);
                        }
                    }
                });
                //加载完成以后做的处理
                $.when(examquestions, zhishidian, infoajax)
                    .done(function () {
                        var flag = 0;
                        for (var key in editdata) {
                            var currValue = editdata[key];
                            if (key == "ETID") {
                                //激发layui的select的选择事件
                                $("#ETID").siblings("div.layui-form-select").find('dl').find('dd[lay-value="' + currValue + '"]').click();
                                if (editdata["ETID"] == 1 || editdata["ETID"] == 2) {
                                    $("#EQAnswerCheck").val(editdata["EQAnswer"]);
                                }
                                else {
                                    $("#EQAnswer").val(editdata["EQAnswer"]);
                                }
                            }
                            else if (key != "EQAnswer"){
                                $("#" + key).val(currValue);
                            }
                        }
                        form.render();
                    });
            })
            //监听提交
            form.on('submit(edit)', function (formdata) {
                formdata.field.EQAnswer = ($("#ETID").val() == 1 || $("#ETID").val() == 2) ? formdata.field.EQAnswerCheck : formdata.field.EQAnswer;
                if (formdata.field.EQAnswer == "") {
                    layer.msg("答案不能为空");
                    return;
                }
                $.ajax({
                    url: ApiUrl + "ExamQuestions/Edit",
                    type: 'post',
                    dataType: 'json',
                    data: formdata.field,
                    success: function (data) {
                        if (data.StatusCode == 200 && data.Count != 0) {
                            layer.alert("修改成功", { icon: 6 }, function (index) {
                                parent.layer.closeAll();
                            })
                        }
                        else if (data.StatusCode == 403) {
                            top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Info);
                        }
                    }
                });
                return false;
            });

        });
    </script>
</body>
</html>